Shader "GJ/MyShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurSize("Blur Size",Float) =1.0
        _TargetColor ("_TargetColor", Color) = (1,1,1,1)
    }
    SubShader
    {
          CGINCLUDE

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            sampler2D _OutlineSource;
            half4 _MainTex_TexelSize;
            float4 _MainTex_ST;
            float _BlurSize;
            fixed4 _TargetColor;
            float _HighlightFlicker=0.0f;

            struct v2f
            {
                float4 pos : SV_POSITION;
                half2 uv[5] : TEXCOORD0;
            };


            struct v2
            {
                float4 pos : SV_POSITION;
                half2  uv: TEXCOORD0;
            };

            v2f vertBlurVertical (appdata_img  v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                half2 uv=v.texcoord;
                o.uv[0]=uv;
                o.uv[1]=uv + float2(0.0,_MainTex_TexelSize.y * 1.0) * _BlurSize;
                o.uv[2]=uv - float2(0.0,_MainTex_TexelSize.y * 1.0) * _BlurSize;
                o.uv[3]=uv + float2(0.0,_MainTex_TexelSize.y * 2.0) * _BlurSize;
                o.uv[4]=uv - float2(0.0,_MainTex_TexelSize.y * 2.0) * _BlurSize;

                return o;
            }

            v2f vertBlurHorizontal(appdata_img v)
            {
               v2f o;
               o.pos = UnityObjectToClipPos(v.vertex);

               half2 uv = v.texcoord;

               o.uv[0] = uv;
               o.uv[1] = uv + float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
               o.uv[2] = uv - float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
               o.uv[3] = uv + float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;
               o.uv[4] = uv - float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;

               return o;
            }

            //融合
            v2 vertBlur(appdata_img v)
            {
               v2 o;
               o.pos = UnityObjectToClipPos(v.vertex);
               o.uv = v.texcoord;
               return o;
            }

            fixed4 fragBlur(v2f i) : SV_Target
            {
               float weight[3] = {0.4026, 0.2442, 0.0545};

               fixed3 sum = tex2D(_MainTex, i.uv[0]).rgb * weight[0];
               fixed4 originalPixel = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv[0], _MainTex_ST));

               for (int it = 1; it < 3; it++) {
                   sum += tex2D(_MainTex, i.uv[it*2-1]).rgb * weight[it];
                   sum += tex2D(_MainTex, i.uv[it*2]).rgb * weight[it];
               }

             return fixed4(sum, 1.0);
            }

            //融合
            fixed4 frag(v2 i) : SV_Target
            {
             fixed3 sum = tex2D(_OutlineSource, i.uv).rgb;
             fixed4 originalPixel = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST));
            // sum = sum.r > 0.95 ? 0 : sum;
             fixed temp = 1.0 - abs((sum.r - 0.5) / 0.5);
             fixed4 target=(1.0- temp)*originalPixel+_TargetColor*(temp);                                  //闪烁
             if(_HighlightFlicker==1.0f)
             target= (1.0- temp)*originalPixel+abs(sin(_Time.y * 1.5f))*_TargetColor*(temp);
             else
             target= (1.0- temp)*originalPixel+_TargetColor*(temp);
             return target;
            }

            ENDCG

            ZTest Always Cull Off ZWrite Off

        Pass
        {
              NAME "GAUSSIAN_BLUR_VERTICAL"

              CGPROGRAM

              #pragma vertex vertBlurVertical
              #pragma fragment fragBlur

              ENDCG
        }

        Pass
        {
            NAME "GAUSSIAN_BLUR_HORIZONTAL"

            CGPROGRAM

            #pragma vertex vertBlurHorizontal
            #pragma fragment fragBlur

            ENDCG
        }

        Pass
        {
            NAME "GAUSSIAN_BLUR"

            CGPROGRAM

            #pragma vertex vertBlur
            #pragma fragment frag

            ENDCG
        }
    }
    FallBack "Diffuse"
}